/**Class Name: NMRReassignmentLeadsBatch 
 * Author: Accenture
 * Requirement/Project Name: NMR
 * Requirement Description: Class for Reassignment of Leads
 */
global class NMRReassignmentLeadsBatch implements Database.Batchable<sObject>{
        public   String query;
        public   Id RecId;
        public Id secondRecId;
        Date currentdate = System.today();
        datetime myDateTime = datetime.now();
    //Query the records from a scheduler    
    global database.querylocator start(Database.BatchableContext BC){
               return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sObject> scope){ 
        List<Lead> leadList = new List<Lead>();  
        Map<Id,Lead> LeadMap = new Map<Id,Lead>();
        Set<String> ZipStr = new Set<String>();
            for(sObject s : scope){
                Lead a = (Lead)s;
                //for leads with days untouched equal to 10
                if(a.Days_Untouched__c == 10){
                    leadList.add(a);
                    LeadMap.put(a.Id,a);
                    ZipStr.add(a.PostalCode);
                }
            } 
            //call the roundrobin logic
            if(leadList.size()>0){
                NMRLeadRoundRobin.executeRoundRobin(leadList,LeadMap,ZipStr);
            }
     }
    global void finish(Database.BatchableContext BC){
          system.debug('finish method: ');
    }
    @isTest(seeAllData=true)
    static void nmrReassignmentLeadsBatchTest(){
        Lead newLead = new Lead();
        newLead.FirstName = 'USS Eisenhower';
        newLead.LastName = 'USS Enterprise';
        newLead.Company = 'Stark Enterprise';
        //newLead.RecordTypeId = leadRecordTypeId ;
        newLead.LeadSource = 'AdStore';
        newLead.Lead_Type__c = 'Inquiry';
        newlead.PostalCode = '56067';
        newlead.Last_Worked__c = System.Now()-10;
        insert newlead;
        Test.StartTest();
        NMRReassignmentLeadsBatch job = new NMRReassignmentLeadsBatch();
        job.RecId = newlead.Id;
        job.query = 'select id,Days_Untouched__c,PostalCode,name,company,LeadSource,Lead_Type__c from Lead where Id =: RecId LIMIT 1';
        ID batchprocessid = Database.executeBatch(job);
        Test.StopTest();
        System.AssertEquals(
           database.countquery('SELECT COUNT()'
              +' FROM Lead WHERE Id=\'' + newlead.Id + '\''),
           1);  
   
   
    }
}